package com.quasar.hpatient.module.home_inspection_chart;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.home_assay_single.SingleItemBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.DimenUtil;

/**
 * description: 首页 ==> 体温
 * created by kalu on 2018/3/29 9:03
 */
public class HomeInspectionChartPresenter implements BasePresenter {

    private final ArrayList<SingleItemBean> mData = new ArrayList<>();

    void assaySingle(final HomeInspectionChartView view, String itemname) {

        if (null == view) return;

        Observable observable = HttpClient.getSingleHolder().getHttpService().assaySingle(HttpParams.assaySingle(itemname));
        request(observable, new OnModelAcceptChangeListener<List<SingleItemBean>>() {
            @Override
            public void modelFail() {
                view.setChartNull();
            }

            @Override
            public void modelSucc(List<SingleItemBean> model) {
                Log.e("---model-------", model.toString());
                if (null == model || model.size() == 0) {
                    view.setChartNull();
                    return;
                }
                mData.addAll(model);
                view.setChart(mData);
                view.setRecycler(true, null, null);
            }
        });
    }


    public void initLayoutData(final HomeInspectionChartView view, String unit) {

        if (null == view) return;

        final BaseCommonAdapter<SingleItemBean> adapter = new BaseCommonAdapter<SingleItemBean>(mData, R.layout.activity_home_inspection_chart_child) {

            @Override
            protected void onNext(RecyclerHolder holder, SingleItemBean model, int position) {
                String inspectiondate = model.getInspectiondate();
                if (!"".equals(inspectiondate) && inspectiondate.length() >= 10) {
                    String substring = inspectiondate.substring(0, 10);
                    view.setText(holder, R.id.activity_home_report_single_child_time, substring);
                    view.setText(holder, R.id.activity_home_report_single_child_value, model.getItemvalue());
                }
            }
        };

        Context context = BaseApp.getContext();
        final View head = LayoutInflater.from(context).inflate(R.layout.activity_home_inspection_chart_head, null);
        final int height = DimenUtil.dp2px(46);
        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        head.setLayoutParams(params);
        final TextView text = head.findViewById(R.id.activity_home_assay_single_head);
        final String str = context.getResources().getString(R.string.home_assay_single_unit, unit);
        text.setText(str);
        adapter.addHead(head);

        adapter.setNullView(context, R.layout.layout_recycler_loading);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(context);
        view.setRecycler(false, adapter, manager);
    }

    @Override
    public void recycler() {
    }
}
